<?php
$orderby = get_major_key(array('id' => 'DESC'), null, 'id');
$_payments = cache_read('payments.php');
$_deliverys = cache_read('deliverys.php');
$oOrder = new Order();

if(!empty($_GET['btnExport'])){	// 导出
	
	$list = $oOrder->get_list('*', $_GET, $orderby, 1, 65536, $offset = 0);
	if(!empty($list['list']) && is_array($list['list'])){
		$data = array();
		foreach($list['list'] as $key => $orderinfo){
			format_state($orderinfo);
			$orderinfo['payment_name'] = $_payments[$orderinfo['payment']]['name'];
			$orderinfo['delivery_name'] = $_deliverys[$orderinfo['delivery']]['name'];
			$orderinfo['sn'] = $orderinfo['sn'].chr(32);
			$orderinfo['addtime'] = ftime($orderinfo['addtime']);
			$data[] = $orderinfo;
		}
		$headers = array(
			'id'				=> array('title' => 'ID', 			'width' => '8'),
			'sn'				=> array('title' => '订单号',		'width' => '18'),
			'username'			=> array('title' => '用户名',		'width' => '12.5'),
			'consignee'			=> array('title' => '收货人',		'width' => '12.5'),
			'fweight'			=> array('title' => '重量（Kg）',	'width' => '12.5'),
			'cost'				=> array('title' => '金额（￥）',	'width' => '12.5'),
			'payment_name'		=> array('title' => '支付方式', 	'width' => '12.5'),
			'payment_fee'		=> array('title' => '支付费用', 	'width' => '12.5'),
			'delivery_name'		=> array('title' => '配送方式', 	'width' => '12.5'),
			'delivery_fee'		=> array('title' => '配送费用', 	'width' => '12.5'),
			'statename'			=> array('title' => '总状态',		'width' => '12.5'),
			'payment_statename'	=> array('title' => '支付状态', 	'width' => '12.5'),
			'delivery_statename'=> array('title' => '配送状态', 	'width' => '12.5'),
			'memo'				=> array('title' => '备注信息', 	'width' => '50',	'wrap' => true),
			'addtime'			=> array('title' => '下单时间', 	'width' => '18'),
		);
		if($list['total'] > 5000){	// 导出超过5000时用CSV
			export_csv($data, $headers, '_E_订单_'.date('Ymd_His').'.csv');
		}else{
			$oExcel = new Excel();
			$oExcel->export($headers, $data, '_E_订单_'.date('Ymd_His').'.xlsx');
		}
	}
	exit;
}

$list = $oOrder->get_list('*', $_GET, $orderby, $__page, $__pagesize);
if(!empty($list['list']) && is_array($list['list'])){
	
	// 主键排序字段
	make_major_key_options(array(
		'id'			=> '下单时间',
		'cost'			=> '订单总价',
	));

	foreach($list['list'] as $key => $orderinfo){
		format_state($orderinfo);
		$orderinfo['paymentinfo'] = $_payments[$orderinfo['payment']];
		$orderinfo['deliveryinfo'] = $_deliverys[$orderinfo['delivery']];
		$orderinfo['addtime'] = ftime($orderinfo['addtime']);
		$orderinfo['updatetime'] = ftime($orderinfo['updatetime']);
		
		// 订单商品
		$goodslist = $oOrder->get_order_goods_all($orderinfo['sn'], $detail = true);
		foreach($goodslist as $k => $goods){
			$goods['goodsinfo']['fweight'] = format_weight_out($goods['goodsinfo']['weight'], $goods['goodsinfo']['weightunit']);
			if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
				if(!empty($goods['goodsinfo']['filename'])){
					$goods['url'] = PUB_DIR.'goods/'.$goods['goodsinfo']['filename'].'.html';
				}else{
					$goods['url'] = PUB_DIR.'goods/'.time_to_path($goods['goodsinfo']['addtime']).'/'.$goods['goodsinfo']['id'].'.html';
				}
			}else{
				$goods['url'] = WEB_DIR."index.php?mdl=goods&do=view&id={$goods['goodsinfo']['id']}";
			}
			$goodslist[$k] = $goods;
		}
		$orderinfo['goodslist'] = $goodslist;
		
		// 收货人信息
		$orderinfo['address'] = $oOrder->get_order_address($orderinfo['sn']);
		$orderinfo['address']['besttime'] = Statics::$delivery_date_arr[$orderinfo['address']['besttime']];
		
		$list['list'][$key] = $orderinfo;
	}
	$vars['list'] = $list['list'];
	$vars['pagelist'] = $list['pagelist'];
}

function format_state(&$orderinfo){
	$statename = '';
	$payment_statename = '';
	$delivery_statename = '';
	
	switch($orderinfo['state']){
		case '-2':
			$statename = '无效';
			break;
		case '-1':
			$statename = '已取消';
			break;
		case '0':
			$statename = '未确认';
			break;
		case '1':
			$statename = '已确认';
			break;
		case '2':
			$statename = '完成';
			break;
		case '3':
			$statename = '已评价';
			break;
	}
	
	switch($orderinfo['payment_state']){
		case '0':
			$payment_statename = '未付款';
			break;
		case '1':
			$payment_statename = '付款中';
			break;
		case '2':
			$payment_statename = '已付款';
			break;
	}
	
	switch($orderinfo['delivery_state']){
		case '0':
			$delivery_statename = '未发货';
			break;
		case '1':
			$delivery_statename = '配货中';
			break;
		case '2':
			$delivery_statename = '已发货';
			break;
		case '3':
			$delivery_statename = '已收货';
			break;
	}
	$orderinfo['statename'] = $statename;
	$orderinfo['payment_statename'] = $payment_statename;
	$orderinfo['delivery_statename'] = $delivery_statename;
}
?>